/**
 * Created by byte on 2016/5/16.
 */
var webpack = require('webpack');
var path = require('path');
var fs = require('fs');
var walk = require('walk')
    , options
    , walker
    ;
var entris = {};
//To be truly synchronous in the emitter and maintain a compatible api,
//the listeners must be listed before the object is created
options = {
    listeners: {
        names: function (root, nodeNamesArray) {
            nodeNamesArray.sort(function (a, b) {
                if (a > b) return 1;
                if (a < b) return -1;
                return 0;
            });
        }
        , directories: function (root, dirStatsArray, next) {
            // dirStatsArray is an array of `stat` objects with the additional attributes
            // * type
            // * error
            // * name

            next();
        }
        , file: function (root, fileStats, next) {
            var key = fileStats.name;
            entris[key.substr(0, key.lastIndexOf('.'))] = path.join(root, fileStats.name);
            next();
        }
        , errors: function (root, nodeStatsArray, next) {
            next();
        }
    }
};

walk.walkSync("./js/", options);
var ProvidePlugin = webpack.ProvidePlugin;
// 分离样式文件
var ExtractTextPlugin = require('extract-text-webpack-plugin');

// 忽略 jquery 插件
// var ignoreFiles = new webpack.IgnorePlugin(/\.\/jquery-last.js$/);

// 判断开发环境
var isProduction = function () {

    return process.env.NODE_ENV === 'production';
};

// 发布文件夹
var outputPath = './public/dist';


var plugins = [
    new webpack.optimize.CommonsChunkPlugin({
        name: 'commons',
        minChunks: 2, //minChunks等于2，说明一个文件至少被require两次才能放在CommonChunk里
        filename: 'javascripts/commons.js',
    }),
    new ExtractTextPlugin('./stylesheets/[name].css'),
    new ProvidePlugin({
       /* $: '$',*/
    }),
    // ignoreFiles
];
if (isProduction()) {
    plugins.push(
        new webpack.optimize.UglifyJsPlugin({
            test: /(\.jsx|\.js)$/,
            exclude: /node_modules/,
            compress: {
                warnings: false
            },
        })
    );
}
var config = {
    target: 'web',
    cache: true,
    entry: entris,
    output: {
        path: path.join(__dirname, outputPath),
        filename: './javascripts/[name].js',
        publicPath: isProduction() ? '' : '',
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: 'babel?presets[]=es2015',
                exclude: /node_modules/
            },
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract(['css', 'sass'])
            },
            {
                test: /\.css$/, loader: ExtractTextPlugin.extract(
                "style-loader",
                "css-loader?sourceMap")
            },
            {
                test: /\.json$/,
                loader: 'json',
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/,
                /*loader: 'file-loader?name=./[path]/[name].[ext]',*/
                loader: 'url-loader?limit=10240&name=./../[path][name].[ext]'

            },
            {
                test: /\.(woff2?|otf|eot|svg|ttf)$/i,
                loader: 'url?name=./[path]/[name].[ext]'
            },
            {
                test: /\.(ogg|mp3)/,
                loader: 'file-loader?name=./sounds/[name].[ext]',
            },
            {
                test: /\.swf/,
                loader: 'file-loader?name=./swf/[name].[ext]',
            },
            {
                test: /\.html/,
                loader: 'file-loader?name=./[path][name].[ext]',
            }
        ],
    },
    plugins: plugins,
    resolve: {
        root: __dirname,
        extensions: ['', '.js']
    },
    devtool: isProduction() ? null : 'source-map',
};

module.exports = config;
